#include "DopplerLineItem.h"
#include <QtGui>
#include <QPainter>
#include <QStyleOptionGraphicsItem>


const int g_nRectSize = 5 ;
static int g_nZValue = 100 ;
DopplerLineItem::DopplerLineItem(const QColor& cColor_, QSize& cSize_)
{
    m_nWidth  = cSize_.width();
    m_nHeight = cSize_.height();
    m_cColor   = cColor_;
    setZValue(g_nZValue);
    SetMoveType(LINE_MOVE_NO) ;
    m_eStype = Qt::SolidLine  ;
}


void DopplerLineItem::SetMoveType(LINE_MOVE_TYPE eType_)
{
    m_eMoveType = eType_  ;
    if(m_eMoveType)
    {
        setFlags(ItemIsSelectable | ItemIsMovable);

        double _nPos1  = m_nWidth / 2.0 ;
        double _nPos2  = m_nHeight/ 2.0 ;
        m_rect.setLeft(_nPos1 - g_nRectSize) ;
        m_rect.setRight(_nPos1 + g_nRectSize);
        m_rect.setTop(_nPos2 - g_nRectSize);
        m_rect.setBottom(_nPos2 + g_nRectSize);
    }
    else
        setFlags(ItemIsPanel);
}


void DopplerLineItem::SetLineStyle(Qt::PenStyle eStype_)
{
    m_eStype = eStype_  ;
    update() ;
}

QRectF DopplerLineItem::boundingRect() const
{
    return QRectF( -g_nRectSize , -g_nRectSize , m_nWidth + 2 * g_nRectSize , m_nHeight + 2 * g_nRectSize);
}

QPainterPath DopplerLineItem::shape() const
{
    QPainterPath path;
    path.addRect(-g_nRectSize, -g_nRectSize , m_nWidth + 2 * g_nRectSize, m_nHeight + 2 * g_nRectSize);
    return path;
}

void DopplerLineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget); // to tell the compiler this parameter is not used, do not expose warning
    //******** to change the background color of when mouse is rect
    QColor fillColor = (option->state & QStyle::State_Selected) ? m_cColor.dark(150) : m_cColor;
    if (option->state & QStyle::State_MouseOver)
        fillColor = fillColor.light(125);
    //******** get the enlarged rate, and by which to determine whether draw the details
    //******** the real display size diliver real size (in pixels)
    //const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
    painter->save();
    QPen _NewPen ;

    _NewPen.setWidth(0);
    _NewPen.setColor(fillColor);
    _NewPen.setStyle(m_eStype);

    painter->setPen(_NewPen);
    painter->drawLine(0, 0 , m_nWidth , m_nHeight );

    if(m_eMoveType)
    {
        _NewPen.setStyle(Qt::SolidLine) ;
        painter->setPen(_NewPen);
        painter->drawRect(m_rect);
    }

    painter->restore();
}

void DopplerLineItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    DopplerGraphicsItem::mousePressEvent(event) ;
    mouseMoveEvent(event) ;
}

void DopplerLineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(!m_eMoveType)  return ;

    QPointF _pos = event->pos()  ;
    QPointF _pos1 = event->scenePos();

    if(m_eMoveType == LINE_MOVE_VERTICAL)
    {
        _pos1.setX(0.0);
        _pos.setY(0);
    }
    if(m_eMoveType == LINE_MOVE_HORIZENTAL)
    {
        _pos1.setY(0.0);
        _pos.setX(0);
    }
    setPos(_pos1) ;

    double _nPos1  = _pos.x() ;
    double _nPos2  = _pos.y() ;
    m_rect.setLeft(_nPos1 - g_nRectSize) ;
    m_rect.setRight(_nPos1 + g_nRectSize);
    m_rect.setTop(_nPos2 - g_nRectSize);
    m_rect.setBottom(_nPos2 + g_nRectSize);
}
